﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Home.ArtDB.WinGui
{
    /// <summary>
    /// 
    /// </summary>
    public class Settings
    {
        /// <summary></summary>
        private const string FILENAME = "settings_mp3db.xml";

        /// <summary></summary>
        public string LastOpenedLibFile = "";

        /// <summary></summary>
        public string LastOpenedLibFolder = "";

        /// <summary></summary>
        public string LastOpenedTargetFolder = "";

        
        /// <summary>
        /// 
        /// </summary>
        public Settings() { }



        /// <summary>
        /// Lädt die Einstellungen aus settings.xml und mapt sie in CSettingsBuilder-Class
        /// </summary>
        /// <returns></returns>
        public Settings Load()
        {
            string settingsFileName = Path.Combine(Application.StartupPath, FILENAME);

            Settings settings = new Settings();
            if (!File.Exists(settingsFileName))
            {
                settings.Save(settingsFileName);
            }

            settings = settings.Load(settingsFileName);

            if (settings.LastOpenedLibFile.Length > 0)
                settings.LastOpenedLibFile=Application.StartupPath[0] + settings.LastOpenedLibFile.Remove(0, 1);

            if (settings.LastOpenedLibFolder.Length > 0)
                settings.LastOpenedLibFolder = Application.StartupPath[0] + settings.LastOpenedLibFolder.Remove(0, 1);

            return settings;
        }

        /// <summary>
        /// Mapt und speichert die Einstellungen aus CSettingsBuilder-Class
        /// </summary>
        public void Save()
        {
            string settingsFileName = Path.Combine(Application.StartupPath, FILENAME);
            this.Save(settingsFileName);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        private void Save(string filename)
        {
            try
            {
                XmlSerializer serializier = new XmlSerializer(this.GetType());

                TextWriter writer = new StreamWriter(filename, false, Encoding.UTF8);
                serializier.Serialize(writer, this);
                writer.Close();
            }
            catch (Exception ex)
            {
                string exc = ex.Message;
                throw (ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private Settings Load(string filename)
        {
            if (!File.Exists(filename))
                return null;

            Settings loadedobj = null;
            try
            {
                XmlSerializer serializier = new XmlSerializer(typeof(Settings));

                TextReader reader = new StreamReader(filename);
                loadedobj = (Settings)serializier.Deserialize(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                string exc = ex.Message;
            }

            return loadedobj;
        }
    }
}
